package org.itboys.mobile.controller.weixin;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.admin.entity.lasted.SystemDictionary;
import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayMemberSessionDto;
import org.itboys.mobile.dto.wallet.SundayAccountDto;
import org.itboys.mobile.dto.wallet.SundayWalletDto;
import org.itboys.mobile.dto.wallet.SundayWalletRecordDto;
import org.itboys.mobile.entity.mysql.wallet.SundayWallet;
import org.itboys.mobile.service.mongo.front.SundayFrontAccountService;
import org.itboys.mobile.service.mysql.front.SundayFrontWalletRecordService;
import org.itboys.mobile.service.mysql.front.SundayFrontWalletService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;


/**
 * Created by zhangyang on 2017/12/19.
 * 圣代_钱包_controller_weixin
 */
@Controller
@RequestMapping("/sunday/weixin/wallet")
public class SundayWeiXinWalletController extends BaseController{

    @Autowired
    private SundayFrontWalletService walletService;

    @Autowired
    private SundayFrontWalletRecordService walletRecordService;



    /**
     * 钱包主页
     * @param request
     * @param
     */
    @RequestMapping(value="/index", method={RequestMethod.GET,RequestMethod.POST})
    public String index(HttpServletRequest request,Model model){
        try{
            Long memberId = AccountIdHoldUtils.getSessionMember(request).getId();
            SundayWalletDto walletDto = walletService.getSingleWalletDto(memberId,true,false,null);
            model.addAttribute("wallet",walletDto);
            return "/aigou/weixin/wallet/index";
        }catch (Exception e){
            logger.error("/sunday/weixin/wallet/index----e="+e.getMessage());
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }

    /**
     * 账户余额/积分/爱购币记录页面
     * @param request
     * @param response
     * @param flag 标识类型:1-余额,2-积分,3-爱购币,4-提成,5提现记录
     * @return
     */
    @RequestMapping(value="/record/index", method={RequestMethod.GET,RequestMethod.POST})
    public String record_index(HttpServletRequest request,
                        HttpServletResponse response,Model model,
                        @RequestParam(value = "flag",required = true) Integer flag
                        ){
        try{
           SundayMemberSessionDto memberSessionDto= AccountIdHoldUtils.getSessionMember(request);
            SundayWalletDto walletDto = walletService.getSingleWalletDto(memberSessionDto.getId(),false,true,flag);
            model.addAttribute("wallet",walletDto);
            model.addAttribute("flag",flag);
            return "/aigou/weixin/wallet/record/index";
        }catch (Exception e){
            logger.error("/sunday/weixin/wallet/record/index----e="+e.getMessage());
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }

    /**
     * 获取账户余额/积分/爱购币记录
     * @param flag 1-余额,2-积分,3-爱购币
     */
    @RequestMapping(value="/record/select", method= {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation("【微信端】获取账户余额/积分/爱购币/提成记录")
    public String record_select(
            HttpServletRequest request,Model model,
            @ApiParam(required = false, name = "flag", value = "1-账户余额,2-积分,3-爱购币,4-提成,5-余额,6-贝壳") @RequestParam(value = "flag",required = false) Integer flag){
        try{
            Long memberId = AccountIdHoldUtils.getSessionMember(request).getId();
            List<SundayWalletRecordDto> records = walletRecordService.getRecord(memberId, flag,null,request);
            model.addAttribute("records",records);
            return "/aigou/weixin/wallet/record/data";
        }catch (Exception e){
            logger.error("/sunday/weixin/wallet/select----e="+e.getMessage());
            e.printStackTrace();
            return null;
        }
    }




    /**
     * 申请提现页面
     * @param request
     * @param response
     * @param
     */
    @RequestMapping(value="/cash/apply", method={RequestMethod.GET,RequestMethod.POST})
    public String cashApply(HttpServletRequest request,
                        HttpServletResponse response,Model model){
        try{
            Long memberId = AccountIdHoldUtils.getSessionMember(request).getId();
            //获取提现账户
            /*List<SundayAccountDto> accounts = cashAccountService.select(memberId);
            SundayWallet wallet = walletService.getByMemberId(memberId);
            model.addAttribute("accounts",accounts);*/
            //可提现金额
            SundayWallet wallet = walletService.getByMemberId(memberId);
            model.addAttribute("wallet",wallet);
            return "/aigou/weixin/wallet/cash/apply";
        }catch (Exception e){
            logger.error("/sunday/weixin/wallet/cash/apply----e="+e.getMessage());
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }

    @RequestMapping(value="/cash/save", method= RequestMethod.POST)
    @ApiOperation(value="【微信端】保存提现记录")
    public void saveApplyCash(HttpServletRequest request,
                           HttpServletResponse response,
                           //@ApiParam(required=true, name="accountId", value="提现账户id")@RequestParam(value = "accountId", required = true)Long accountId,
                           @ApiParam(required=true, name="money", value="提现金额")@RequestParam(value = "money", required = true)BigDecimal money){
        try{
            Long memberId = AccountIdHoldUtils.getSessionMember(request).getId();
            walletService.saveCash(money,memberId,SundayCommonConstants.pay_type_wechatpay_html);
            ResponseMessage.success();
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/wallet/cash/save----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @ApiOperation(value="【微信端】贝壳充值主页面")
    @RequestMapping(value="/conch/charge/index", method={RequestMethod.GET,RequestMethod.POST})
    public String conch_charge_index(HttpServletRequest request,
                            HttpServletResponse response,Model model){
        try{
            Long memberId = AccountIdHoldUtils.getSessionMember(request).getId();
            //当前余额
            SundayWallet wallet = walletService.getByMemberId(memberId);
            model.addAttribute("wallet",wallet);
            return "/aigou/weixin/wallet/conch/charge/index";
        }catch (Exception e){
            logger.error("/sunday/weixin/wallet/conch/charger/index----e="+e.getMessage());
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }
    @RequestMapping(value="/conch/charge", method= RequestMethod.POST)
    @ApiOperation(value="【微信端】贝壳充值")
    public void conch_charge(HttpServletRequest request,
                              HttpServletResponse response,
                              //@ApiParam(required=true, name="accountId", value="提现账户id")@RequestParam(value = "accountId", required = true)Long accountId,
                              @ApiParam(required=true, name="number", value="贝壳编码")@RequestParam(value = "number", required = true)String number){
        try{
            Long memberId = AccountIdHoldUtils.getSessionMember(request).getId();
            walletService.conch_charge(number,memberId);
            ResponseMessage.success();
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/wallet/conch/charge----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
}
